import * as THREE from "three";
import EShapeType from "./ESharpType";
import IShape from "./IShape";
import Park from "./park";
import ParkingLot from "./parkingLot";
import Pillar from "./pillar";

class Render {
  private scene!: THREE.Scene;
  private camera!: THREE.Camera;
  private paLot!: ParkingLot;

  private renderPillars: (pillars: Pillar[]) => void = (pillars) => {
    pillars.forEach((pi) => {
      const { width, height, pos, yHeight } = pi;
      console.log(pos, width, height);
      let geometry = new THREE.BoxGeometry(width, yHeight, height);
      let material = new THREE.MeshBasicMaterial({ color: 0x222222 });
      let mesh = new THREE.Mesh(geometry, material);
      mesh.castShadow = true;
      mesh.position.set(pos.x, 0, pos.y);
      this.scene.add(mesh);
      console.log("render a pillar");
    });
  };

  private renderFloors: (floors: IShape[]) => void = (floors) => {
    floors.forEach((fl) => {
      const { type, data } = fl;
      if (type === EShapeType.RECT) {
        const { width, height } = data as { width: number; height: number };
        const yHeight = 10;
        let geometry = new THREE.BoxGeometry(width, yHeight, height);
        let material = new THREE.MeshBasicMaterial({ color: 0xdddddd });
        let mesh = new THREE.Mesh(geometry, material);
        mesh.receiveShadow = true;
        mesh.position.setY(-yHeight);
        this.scene.add(mesh);
        console.log("render a floor");
      }
    });
  };

  private renderParks: (parks: Park[]) => void = (parks) => {
    parks.forEach((park) => {
      const { width, height, pos } = park;
      const yHeight = 10;
      let geometry = new THREE.BoxGeometry(width, yHeight, height);
      let material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
      let mesh = new THREE.Mesh(geometry, material);
      mesh.position.set(pos.x, 0, pos.y);
      this.scene.add(mesh);
      console.log("render a park");

      this.camera.lookAt(mesh.position);
    });
  };

  constructor(scene: THREE.Scene, camera: THREE.Camera) {
    this.scene = scene;
    this.camera = camera;
  }

  init: (paLot: ParkingLot) => void = (paLot) => {
    this.paLot = paLot;
    const { parks, floors, pillars } = paLot;

    this.renderFloors(floors);
    this.renderParks(parks);
    this.renderPillars(pillars);
  };
}

export default Render;
